package com.tengju.data.infrastructure.repository.convert;

import com.tengju.data.domain.model.report.*;
import com.tengju.data.domain.model.user.StarLevelEnum;
import com.tengju.data.domain.model.user.UserId;
import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.data.domain.model.user.UserLevelEnum;
import com.tengju.data.domain.shared.DateUtil;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.*;
import com.tengju.data.interfaces.api.dto.StoreMarketProfitForm;
import com.tengju.data.interfaces.api.vo.MarketProfitMidInfoVO;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Optional;

public class AchievementBonusConvert {

    private AchievementBonusConvert() {
    }

    public static AchievementBonusDO convertAchievementBonusToDO(AchievementBonus achievementBonus) {
        AchievementBonusDO achievementBonusDO = new AchievementBonusDO();
        achievementBonusDO.setIdCode(achievementBonus.getIdCode().getValue());
        achievementBonusDO.setpIdCode(achievementBonus.getPIdCode() != null ? achievementBonus.getPIdCode().getValue() : 0);
        achievementBonusDO.setSettlePIdCode(achievementBonus.getSettlePIdCode() != null ? achievementBonus.getSettlePIdCode().getValue() : 0);
        achievementBonusDO.setUserId(achievementBonus.getUserId() != null ? achievementBonus.getUserId().getValue() : null);
        achievementBonusDO.setpUserId(achievementBonus.getPUserId() != null ? achievementBonus.getPUserId().getValue() : null);
        achievementBonusDO.setSettlePUserId(achievementBonus.getSettlePUserId() != null ? achievementBonus.getSettlePUserId().getValue() : null);
        achievementBonusDO.setSelfCancelAchievement(achievementBonus.getSmallTeamCancelBeforeAchievement());
        achievementBonusDO.setCancelAchievement(achievementBonus.getAllTeamCancelBeforeAchievement());
        achievementBonusDO.setTheoryAchievement(achievementBonus.getTheoryAchievement());
        achievementBonusDO.setAchievement(achievementBonus.getAchievement());
        achievementBonusDO.setFactor(achievementBonus.getFactor());
        achievementBonusDO.setProfit(achievementBonus.getProfit());
        achievementBonusDO.setTotalProfit(achievementBonus.getTotalProfit());
        achievementBonusDO.setMonth(achievementBonus.getMonth());
        achievementBonusDO.setStatus(achievementBonus.getStatus().getStatus());
        achievementBonusDO.setTeamRatioType(achievementBonus.getTeamRatioType());
        achievementBonusDO.setStarRole(achievementBonus.getStarRole());
        achievementBonusDO.setStarLevel(achievementBonus.getStarLevel());
        achievementBonusDO.setUserTag(achievementBonus.getUserTag());
        achievementBonusDO.setVersion(achievementBonus.getVersion());
        achievementBonusDO.setSettleAchievement(achievementBonus.getSettleAchievement());
        return achievementBonusDO;
    }

    public static StoreMarketProfitForm convertAchievementBonusToForm(AchievementBonus achievementBonus) {
        StoreMarketProfitForm storeMarketProfitForm = new StoreMarketProfitForm();
        storeMarketProfitForm.setIdCode(achievementBonus.getIdCode().getValue());
        storeMarketProfitForm.setPIdCode(achievementBonus.getPIdCode() != null ? achievementBonus.getPIdCode().getValue() : 0);
        storeMarketProfitForm.setSettlePIdCode(achievementBonus.getSettlePIdCode() != null ? achievementBonus.getSettlePIdCode().getValue() : 0);
        storeMarketProfitForm.setSelfCancelAchievement(achievementBonus.getSmallTeamCancelBeforeAchievement().doubleValue());
        storeMarketProfitForm.setCancelAchievement(achievementBonus.getAllTeamCancelBeforeAchievement().doubleValue());
        storeMarketProfitForm.setTheoryAchievement(achievementBonus.getTheoryAchievement().doubleValue());
        storeMarketProfitForm.setAchievement(achievementBonus.getAchievement().doubleValue());
        storeMarketProfitForm.setFactor(achievementBonus.getFactor());
        storeMarketProfitForm.setProfit(achievementBonus.getProfit().doubleValue());
        storeMarketProfitForm.setTotalProfit(achievementBonus.getTotalProfit().doubleValue());
        storeMarketProfitForm.setMonth(DateUtil.formatMonthToStr(achievementBonus.getMonth()));
        storeMarketProfitForm.setStatus(achievementBonus.getStatus().getStatus());
        storeMarketProfitForm.setTeamRatioType(achievementBonus.getTeamRatioType());
        storeMarketProfitForm.setUserTag(achievementBonus.getUserTag());
        storeMarketProfitForm.setVersion(achievementBonus.getVersion());
        return storeMarketProfitForm;
    }

    public static AchievementBonusCalMidInfo convertExternalMidInfoVOToEntity(MarketProfitMidInfoVO marketProfitMidInfoVO,Integer month) {
        return new AchievementBonusCalMidInfo(
                UserIdCode.of(marketProfitMidInfoVO.getIdCode()),
                UserIdCode.of(marketProfitMidInfoVO.getPIdCode()),
                month,
                UserLevelEnum.getByCode(marketProfitMidInfoVO.getLevel().intValue()),
                null,
                LocalDateTime.ofInstant(Instant.ofEpochMilli(marketProfitMidInfoVO.getLogTime()), ZoneId.systemDefault()),
                marketProfitMidInfoVO.getTeamRatioType(),
                BigDecimal.valueOf(marketProfitMidInfoVO.getSmallTeamMoneyOut()),
                BigDecimal.valueOf(marketProfitMidInfoVO.getSelfMoneyOut()),
                BigDecimal.valueOf(marketProfitMidInfoVO.getSmallTeamCancelMoneyOut()),
                BigDecimal.valueOf(marketProfitMidInfoVO.getSmallTeamCancelBeforeMoneyOut()),
                AchievementBonusCalTypeEnum.parseByType(marketProfitMidInfoVO.getCalType())
        );
    }


    public static AchievementBonus convertToModel(AchievementBonusDO achievementBonusDO) {

        return new AchievementBonus(
                Optional.ofNullable(achievementBonusDO.getIdCode())
                        .filter(l -> l > 0)
                        .map(UserIdCode::new).orElse(null),
                Optional.ofNullable(achievementBonusDO.getpIdCode())
                        .filter(l -> l > 0)
                        .map(UserIdCode::new).orElse(null),
                Optional.ofNullable(achievementBonusDO.getSettlePIdCode())
                        .filter(l -> l > 0)
                        .map(UserIdCode::new).orElse(null),
                achievementBonusDO.getUserId() == null ? null : new UserId(achievementBonusDO.getUserId()),
                achievementBonusDO.getpUserId() == null ? null : new UserId(achievementBonusDO.getpUserId()),
                achievementBonusDO.getSettlePUserId() == null ? null : new UserId(achievementBonusDO.getSettlePUserId()),
                achievementBonusDO.getTeamRatioType(),
                achievementBonusDO.getFactor(),
                achievementBonusDO.getProfit(),
                achievementBonusDO.getTotalProfit(),
                achievementBonusDO.getMonth(),
                achievementBonusDO.getSelfCancelAchievement(),
                achievementBonusDO.getCancelAchievement(),
                achievementBonusDO.getTheoryAchievement(),
                achievementBonusDO.getAchievement(),
                achievementBonusDO.getSettleAchievement(),
                AchievementBonusStatusEnum.parseByStatus(achievementBonusDO.getStatus()),
                achievementBonusDO.getStarRole(),
                achievementBonusDO.getStarLevel(),
                achievementBonusDO.getUserTag(),
                achievementBonusDO.getVersion()
        );
    }

    public static AchievementBonusSum convertToModel(AchievementBonusSumDO achievementBonusSumDO) {
        return new AchievementBonusSum(new UserIdCode(achievementBonusSumDO.getIdCode()),
                AchievementBonusStatusEnum.parseByStatus(achievementBonusSumDO.getStatus()),
                achievementBonusSumDO.getMonth(),
                achievementBonusSumDO.getProfit(),
                achievementBonusSumDO.getTotalProfit(),
                achievementBonusSumDO.getAchievement()
        );
    }

    public static UserBonusResult convertMarketBonusResultDOToEntity(UserBonusResultDO userBonusResultDO) {
        return new UserBonusResult(
                UserIdCode.of(userBonusResultDO.getIdCode()),
                userBonusResultDO.getBonus(),
                userBonusResultDO.getMonth(),
                StarLevelEnum.parseByStarLevel(userBonusResultDO.getStarLevel())
        );
    }

    public static AchievementBonusLog convertToLogModel(AchievementBonusLogDO achievementBonusLogDO) {
        if (achievementBonusLogDO == null) {
            return null;
        }
        return new AchievementBonusLog(
                Optional.ofNullable(achievementBonusLogDO.getIdCode())
                        .filter(l -> l > 0)
                        .map(UserIdCode::new).orElse(null),
                Optional.ofNullable(achievementBonusLogDO.getpIdCode())
                        .filter(l -> l > 0)
                        .map(UserIdCode::new).orElse(null),
                Optional.ofNullable(achievementBonusLogDO.getSettlePIdCode())
                        .filter(l -> l > 0)
                        .map(UserIdCode::new).orElse(null),
                achievementBonusLogDO.getUserId() == null ? null : new UserId(achievementBonusLogDO.getUserId()),
                achievementBonusLogDO.getpUserId() == null ? null : new UserId(achievementBonusLogDO.getpUserId()),
                achievementBonusLogDO.getSettlePUserId() == null ? null : new UserId(achievementBonusLogDO.getSettlePUserId()),
                achievementBonusLogDO.getTeamRatioType(),
                achievementBonusLogDO.getFactor(),
                achievementBonusLogDO.getProfit(),
                achievementBonusLogDO.getTotalProfit(),
                achievementBonusLogDO.getMonth(),
                achievementBonusLogDO.getSelfCancelAchievement(),
                achievementBonusLogDO.getCancelAchievement(),
                achievementBonusLogDO.getTheoryAchievement(),
                achievementBonusLogDO.getAchievement(),
                achievementBonusLogDO.getSettleAchievement(),
                AchievementBonusStatusEnum.parseByStatus(achievementBonusLogDO.getStatus()),
                achievementBonusLogDO.getStarRole(),
                achievementBonusLogDO.getStarLevel(),
                achievementBonusLogDO.getUserTag(),
                achievementBonusLogDO.getVersion(),
                DateUtil.dateToLocalDateTime(achievementBonusLogDO.getLogTime())
        );
    }

    public static AchievementBonusCalMidInfo convertMidInfoVOToEntity(AchievementBonusCalMidInfoDO achievementBonusCalMidInfoDO) {
        if (achievementBonusCalMidInfoDO == null) {
            return null;
        }
        return new AchievementBonusCalMidInfo(
                UserIdCode.of(achievementBonusCalMidInfoDO.getIdCode()),
                UserIdCode.of(achievementBonusCalMidInfoDO.getpIdCode()),
                achievementBonusCalMidInfoDO.getMonth(),
                UserLevelEnum.getByCode(achievementBonusCalMidInfoDO.getLevel().intValue()),
                DateUtil.dateToLocalDateTime(achievementBonusCalMidInfoDO.getLevelChangeTime()),
                DateUtil.dateToLocalDateTime(achievementBonusCalMidInfoDO.getLogTime()),
                achievementBonusCalMidInfoDO.getTeamRatioType(),
                achievementBonusCalMidInfoDO.getSmallTeamMoneyOut(),
                achievementBonusCalMidInfoDO.getSelfMoneyOut(),
                achievementBonusCalMidInfoDO.getSmallTeamCancelMoneyOut(),
                achievementBonusCalMidInfoDO.getSmallTeamCancelBeforeMoneyOut(),
                AchievementBonusCalTypeEnum.parseByType(achievementBonusCalMidInfoDO.getCalType())
        );
    }
}
